package com.augurit.common.system.web.org;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.augurit.common.base.action.ExtCrudActionSupport;
import com.augurit.common.system.service.org.IAcApplicationService;
import com.augurit.common.system.service.org.IAcFunctionService;
import com.augurit.common.system.service.org.IAcRoleService;
import com.augurit.common.system.service.org.IAcServerService;
import com.augurit.common.system.util.WebContextUtils;
import com.augurit.common.system.web.org.form.AcFunctionForm;
import com.augurit.common.util.ExtUtils;

@SuppressWarnings("serial")
@Results({
	@Result(name = ExtCrudActionSupport.RELOAD, location = "ac-function.action", type = "redirect"),
	@Result(name = ExtCrudActionSupport.SUCCESS, location = "/common/system/org/ac-function.jsp")
})
public class AcFunctionAction extends ExtCrudActionSupport<AcFunctionForm> {
	
	@Autowired
	private IAcServerService acServerService;
	
	@Autowired
	private IAcApplicationService acApplicationService;
	
	@Autowired
	private IAcFunctionService functionService;
	
	@Autowired
	private IAcRoleService acRoleService;
	
	// 属性
	private AcFunctionForm form;
	private Long id;
	private Long[] ids;
	private Page<AcFunctionForm> page = new Page<AcFunctionForm>(15);
	private Long moduleId;
	private Long funcId;
	private Long[] roleIds;
	
	
	/**
	 * 功能查询
	 */
	@Override
	public String list() throws Exception {
		ExtUtils.initPageFromExtGridParam(this.getRequest(), page);
		functionService.getFuncs(page, form, 
				WebContextUtils.getAcApplicationMap(
						acServerService, acApplicationService, this.getApplication()),
				WebContextUtils.getSysCodeBuffer(this.getApplication()));
		this.extRenderGridJson(page);
		return null;
	}
	
	/**
	 * 获取指定模块的直属功能列表
	 */
	public String getFuncByModuleId() throws Exception {
		ExtUtils.initPageFromExtGridParam(this.getRequest(), page);
		functionService.getFuncsByModuleId(moduleId, page, 
				WebContextUtils.getAcApplicationMap(
						acServerService, acApplicationService, this.getApplication()),
				WebContextUtils.getSysCodeBuffer(this.getApplication()));
		this.extRenderGridJson(page);
		return null;
	}

	/**
	 * 新增或编辑功能页面
	 */
	@Override
	public String input() throws Exception {
		this.extRenderFormJson(form);
		return null;
	}
	
	/**
	 * 保存功能信息
	 */
	@Override
	public String save() throws Exception {
		functionService.addFuncCascade(form);
		this.extRenderSuccess();
		return null;
	}
	
	/**
	 * 删除一个或多个功能
	 */
	@Override
	public String delete() throws Exception {
		functionService.deleteFuncCascade(ids);
		this.extRenderSuccess();
		return null;
	}
	
	/**
	 * 使功能不与指定角色关联
	 * @param funcId:功能id, roleId:角色id
	 * @return
	 * @throws Exception
	 */
	public String dissociateFuncFromRole()throws Exception{
		if(roleIds != null && roleIds.length > 0){
			for(Long roleId: roleIds)
				acRoleService.disAssociateFuncFromRole(roleId, funcId);
		}
		this.extRenderSuccess();
		return null;
	}
	
	
	public AcFunctionForm getModel() {
		return form;
	}

	@Override
	protected void prepareModel() throws Exception {
		if(id != null){
			form = functionService.get(id);
		}
		else{
			form = new AcFunctionForm();
		}

	}
	
	// 属性getter和setter方法
	public AcFunctionForm getForm() {
		return form;
	}

	public void setForm(AcFunctionForm form) {
		this.form = form;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long[] getIds() {
		return ids;
	}

	public void setIds(Long[] ids) {
		this.ids = ids;
	}

	public Long getModuleId() {
		return moduleId;
	}

	public void setModuleId(Long moduleId) {
		this.moduleId = moduleId;
	}

	public Long getFuncId() {
		return funcId;
	}

	public void setFuncId(Long funcId) {
		this.funcId = funcId;
	}

	public Long[] getRoleIds() {
		return roleIds;
	}

	public void setRoleIds(Long[] roleIds) {
		this.roleIds = roleIds;
	}
}
